package com.ekingstar.lss.commons.supper.utils.query.simple;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.ekingstar.lss.commons.exceptions.QueryUtilsException;
import com.ekingstar.lss.commons.supper.utils.query.LssCondition;
import com.ekingstar.lss.commons.utils.StringUtils;

/**
 *	公司：金仕达多媒体
 *	作者：童弘成
 *	时间：2011-4-21 上午09:48:57
 *	描述：条件简单实现
 */
public class SimpleCondition implements Serializable, LssCondition {
	private static final long serialVersionUID = -4895507488521640641L;
	
	public static Pattern typecheck=Pattern.compile("^((and)|(or))$");
	
	private List<LssCondition> conditions;
	
	private List<Object> values;
	
	
	private String content;
	
	private String type;
	
	public SimpleCondition(){}
	
	public SimpleCondition(List<LssCondition> conditions,String type){
		if(conditions==null){
			throw new NullPointerException("子条件不能为空");
		}
		if(conditions.size()==0){
			throw new QueryUtilsException("子条件项目不能为零");
		}
		if(!typecheck.matcher(type).matches()){
			throw new QueryUtilsException("条件连接类型必须是and或者or");
		}
		this.conditions=conditions;
		this.type=type;
	}
	
	public SimpleCondition(String content,List<Object> values,String type){
		if(!typecheck.matcher(type).matches()){
			throw new QueryUtilsException("条件连接类型必须是and或者or");
		}
		this.content=content;
		this.values=values;
		this.type=type;
	}
	
	public boolean checkCondition() {
		if(this.content==null){
			if(this.conditions==null){
				return false;
			}
			for (int i = 0; i < this.conditions.size(); i++) {
				if(!this.conditions.get(i).checkCondition()){
					return false;
				}
			}
		}else if(this.conditions==null){
			
			int count1=StringUtils.countOfString(this.content, "\\?");
			System.out.println(count1);
			if(count1!=0){
				if(this.values==null){
					return false;
				}
				if(this.values.size()!=count1){
					return false;
				}
			}
		}else{
			return false;
		}
		return true;
	}

	public String getContent() {
		if(this.content!=null&&!"".equals(this.content)){
			return this.content;
		}else{
			StringBuffer contents=new StringBuffer();
			contents.append("(");
			for (int i = 0; i < this.conditions.size(); i++) {
				if(i==0){
					contents.append(this.conditions.get(i).getContent());
				}else{
					contents.append(" ").append(this.conditions.get(i).getType());
					contents.append(" ").append(this.conditions.get(i).getContent());
				}
			}
			contents.append(")");
			
			return contents.toString();
		}
	}

	public String getType() {
		return this.type;
	}
	
	/**
	 * 设置语句连接类型
	 * @param type and或者or
	 */
	public void setType(String type){
		if(!typecheck.matcher(type).matches()){
			throw new QueryUtilsException("条件连接类型必须是and或者or");
		}
		this.type=type;
	}
	
	public List<Object> getValues() {
		if(this.content!=null){
			return this.values;
		}else{
			List<Object> tvalues=new ArrayList<Object>();
			for (int i = 0; i < this.conditions.size(); i++) {
				tvalues.addAll(this.conditions.get(i).getValues());
			}
			return tvalues;
		}
	}

	/**
	 * 设置查询条件参数。
	 * @param values
	 */
	public void setValues(List<Object> values){
		if(this.content!=null){
			this.values=values;
		}else{
			throw new QueryUtilsException("有子查询条件不能输入参数值");
		}
	}
	
	/**
	 * 设置条件体
	 * @param content 条件体文本
	 * @throws QueryUtilsException 如果此条件包含自条件则抛出
	 */
	public void setContent(String content){
		if(this.isMulti()){
			throw new QueryUtilsException("多条件不能设置条件文本");
		}
		this.content=content;
	}
	
	
	public void setConditions(List<LssCondition> conditions){
		this.content=null;
		this.conditions=conditions;
	}
	public boolean isMulti() {
		if(this.conditions!=null&&this.conditions.size()>0){
			return true;
		}
		return false;
	}
}
